﻿
// First: Create config
ko.validation.configure({
	registerExtenders: true,
	messagesOnModified: true,
	insertMessages: true,
	parseInputAttributes: true,
	messageTemplate: null,
	decorateElement: true,
	errorClass: "error",
	errorMessageClass: "error-desc"
});
var mustEqual = function (val, other) {
	return val == other();
};

// Second: Create new model to apply to view
var viewModel = new function () {
    // About you
    this.SiteName = ko.observable(data.WebsiteSettingInfo.SiteName).extend({ maxLength: 250 }).extend({ required: { message: "Tên trang là bắt buộc"} });
    this.SiteSource = ko.observable(data.WebsiteSettingInfo.SiteSource);
    this.MapName = ko.observable(data.WebsiteSettingInfo.MapName);
    this.MapImageURL = ko.observable(data.WebsiteSettingInfo.MapImageURL);
    this.MapLink = ko.observable(data.WebsiteSettingInfo.MapLink);

    // Submit, Next button
    this.InvalidateForm = ko.observable(false);
    this.SubmitForm = function () {
        if (viewModel.errors().length == 0) {
            $('form').submit();
        }
        else {
            viewModel.errors.showAllMessages();
            this.InvalidateForm(true);
        }
    }
    this.SyschronizeSuccess = ko.observable("");
    this.SynchronizeCategory = function () {
        var url = "/Management/SystemSettings/Synchronize?type=category";
        // Json Get function to retrive data from server
        //this.SyschronizeSuccess = ko.observable("");
        $.getJSON(url, function (data) {
            viewModel.SyschronizeSuccess("Đồng bộ dữ liệu danh mục thành công");
        });
    };
    this.SynchronizeState = function () {
        var url = "/Management/SystemSettings/Synchronize?type=state";
        // Json Get function to retrive data from server
        //this.SyschronizeSuccess = ko.observable("");
        $.getJSON(url, function (data) {
            viewModel.SyschronizeSuccess("Đồng bộ dữ liệu vùng/miền thành công");
        });
    }
};

viewModel.errors = ko.validation.group(viewModel);

// Third: Apply view model to view to handle action when user input data
ko.validation.rules.pattern.message = 'Invalid.';
ko.applyBindings(viewModel, document.getElementById("main"));